const http = require('http')
const server = http.createServer()
const fs =require('fs')
const path = require('path')

server.listen(3000 ,()=>{
    console.log('服务器启动成功了,请访问: http://localhost:3000/index.html');
})

server.on('request',(req,res)=>{
    const filePath=path.join(__dirname,'www',req.url)
    fs.readFile(filePath,(err,data)=>{
        if(err){
       fs.readFile(path.join(__dirname,'www','404.html'),'utf8',(err,data)=>{
           res.statusCode=404
           res.end(data)
       })
       return
        }
        res.end(data)
    })
})
